/*
 * Copyright (c) 2002-2006 Universidade Federal de Campina Grande This program
 * is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version. This
 * program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.ourgrid.worker.collectors.gmond;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.ourgrid.common.spec.worker.WorkerSpec;
import org.ourgrid.worker.collectors.common.Metric;
import org.ourgrid.worker.collectors.interfaces.WorkerCollectorController;
import org.ourgrid.worker.dao.WorkerSpecDAO;

import br.edu.ufcg.lsd.commune.api.InvokeOnDeploy;
import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;

/**
 * This entity collects all metrics provided by gmond daemon and sends these metrics to all
 * interested entities.
 * 
 * @author Leonardo de Assis - leonardo@ourgrid.org
 */
public class GMondCollectorController implements WorkerCollectorController {
	
	private ServiceManager serviceManager;
	
	@InvokeOnDeploy
	public void init(ServiceManager serviceManager) {
		this.serviceManager = serviceManager;
	}
	
	/**
	 * @return the serviceManager
	 */
	protected ServiceManager getServiceManager() {
		return serviceManager;
	}
	
	/**
	 * Informs to all interested clients that metrics changed.
	 *
	 */
	public void metricsChanged(List<Metric> gmondMetrics ) {
		List<Metric> clone = new LinkedList<Metric>( gmondMetrics );
		Map<String,String> attributes = new HashMap<String,String>();
		for (Metric gmondMetric : clone) {
			attributes.put(gmondMetric.getName(), gmondMetric.getValue());
		}
		
		WorkerSpec spec = getServiceManager().getDAO(WorkerSpecDAO.class).getWorkerSpec();
		
		spec.putAttributes(attributes);
		
	}

	public void startDaemon() {
		
	}
	
}
